package com.jic.purchase.controller;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.purchase.constant.consist.ResponseCode;
import com.jic.purchase.model.entity.ContractPerson;
import com.jic.purchase.model.vo.request.ContractPersonRequest;
import com.jic.purchase.model.vo.response.ContractPersonResponse;
import com.jic.purchase.service.ContractPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
/**
 * @Author: tzc
 * @date: 2020-10-04 03:43:24
 */
 @RestController
 @Slf4j
 @RequestMapping("/contractPerson")
 @Api(tags = " [合同联系人]控制类-PC")
public class ContractPersonController {

	@Resource
	private ContractPersonService service;
	
	
     /**
      *新增记录
      * @param request 新增[合同联系人]请求参数
      * @return ContractPersonResponse 新增[合同联系人]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[合同联系人]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ContractPersonRequest request) throws Throwable {
     		ContractPerson contractPerson = new ContractPerson();
    		BeanUtils.copyProperties(request, contractPerson);
    	    long flag = service.save(contractPerson);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[合同联系人]请求参数
      * @return ContractPersonResponse 新增或修改[合同联系人]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[合同联系人]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody ContractPersonRequest request) throws Throwable {
      		ContractPerson contractPerson = new ContractPerson();
		     BeanUtils.copyProperties(request, contractPerson);
	         long flag =   service.saveOrUpdate(contractPerson);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[合同联系人]请求参数
      * @return ContractPersonResponse 删除[合同联系人]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[合同联系人]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ContractPersonRequest request) throws Throwable {
      		ContractPerson contractPerson = new ContractPerson();
	     	BeanUtils.copyProperties(request, contractPerson);
	         long flag = service.deleteByPrimaryKeyLogically(contractPerson);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[合同联系人]请求参数
      * @return ContractPersonResponse 修改[合同联系人]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[合同联系人]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ContractPersonRequest request) throws Throwable {
      		ContractPerson contractPerson = new ContractPerson();
	     	BeanUtils.copyProperties(request, contractPerson);
	         long flag = service.updateByPrimaryKeySelective(contractPerson);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[合同联系人]详情请求参数
      * @return ContractPersonResponse 查询[合同联系人]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[合同联系人]详情")
      @ResponseBody
     public RestResult<ContractPersonResponse> getByPrimaryKey(@Validated  @RequestBody ContractPersonRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ContractPerson contractPerson = new ContractPerson();
	     	BeanUtils.copyProperties(request, contractPerson);
	     	ContractPerson result = service.getByPrimaryKey(contractPerson);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ContractPersonResponse bo = new ContractPersonResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ContractPersonResponse());
     }
	
//    /**
//      * 分页查询
//      *
//      * @param request 分页查询请求参数
//      * @return 分页列表
//      * @throws Throwable 业务异常和系统异常
//      */
//     @PostMapping("/listPage")
//     @ApiOperation(value = " 分页查询列表 ")
//     @ResponseBody
//     public RestResult<PageInfo<ContractPerson>> listPage (@RequestBody ContractPersonRequest request) throws Throwable {
//      ContractPerson param = new ContractPerson();
//      BeanUtils.copyProperties(request, param);
//      param.setDeleteFlag(0);
//     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
//     }
     
	
//      /**
//       *CONTRACT_PERSON 查询列表（不分页）
//       * @param  request 前端请求参数
//       */
//      @PostMapping("/listAll")
//      @ApiOperation(value = "不分页查询 [合同联系人] ")
//      @ResponseBody
//      public RestResult<List<ContractPersonResponse>>  listContractPersonAllRecord(@RequestBody ContractPersonRequest request) throws Throwable {
//      		ContractPerson param = new ContractPerson();
//	     	BeanUtils.copyProperties(request, param);
//	     	param.setDeleteFlag(0);
//	     	List<ContractPerson> list = service.listAllRecord(param);
//			List<ContractPersonResponse> responseList = list.stream().map(temp -> {
//			ContractPersonResponse bo = new ContractPersonResponse();
//			BeanUtils.copyProperties(temp, bo);
//			return bo;
//			}).filter(item -> item != null).collect(Collectors.toList());
//		    return RestResult.success(responseList);
//      }

     }

	
